package com.zx.mes.longsecurity.hyl.config;

import com.zx.mes.hyl.common.util.MD5Util;
import com.zx.mes.longsecurity.hyl.service.impl.CustomUserService;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * 配置类
 * @author hyl
 * @date 2017-12-6
 */
@EnableWebSecurity
@Configuration
/**
 * 控制权限注解
 */
@EnableGlobalMethodSecurity(prePostEnabled  = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private Logger logger=Logger.getLogger(SecurityConfig.class);

    @Bean
    UserDetailsService customUserService(){
        return new CustomUserService();
    }

    /**
     * 不加密
     * auth.userDetailsService(customUserService());
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {


        /**
         * 加密
         */
        auth.userDetailsService(customUserService()).passwordEncoder(new PasswordEncoder() {
            /**
             * 返回加密之后的密码
             * @param rawPassword 前台传送过来的password
             * @return string
             */
            @Override
            public String encode(CharSequence rawPassword) {
                logger.info("前台传过来的密码:"+rawPassword);
                return MD5Util.md5((String) rawPassword);
            }

            @Override
            public boolean matches(CharSequence rawPassword, String encodedPassword) {
                logger.info("前台传过来的密码:"+rawPassword+" encodedPassword:"+encodedPassword);
                return encodedPassword.equals(MD5Util.md5((String) rawPassword));
            }
        });
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest().authenticated()
//                .antMatchers("/js/**", "/index").permitAll()
                .and()
                //解决X-Frame-Options deny 造成的页面空白
                .headers().frameOptions().disable()
                .and()
                .formLogin().loginPage("/login").failureUrl("/login-error").permitAll()
                .and()
                .logout().permitAll();

        // 关闭csrf
        http.csrf().disable();

    }


    @Override
    public void configure(WebSecurity web) throws Exception {
        //解决静态资源被拦截的问题
        web.ignoring().antMatchers("/X-admin/**","/js/**");

    }

}
